import React from 'react';
import _ from 'lodash';
import * as apis from '@/services/MonitorMgr';
import { Statistic, Tooltip } from 'antd';
import { dataMartMetrics } from '../config';

// key_status 预警状态
// 圆点状态(warning-预警标准-红色,remind-提醒标准-黄色,health-健康标准-绿色,none-没有)
// produceVo 产
// saleVo 销
// statementVo 结
// stockVo 存
// storeVo 储
// supplyVo 供
// 11 旭辉集团,20 事业部/区域集团,21 城市公司,23 直管项目,31 项目,32 分期
const setStatistic = (val, divisor, decimal, suffix, warningState) => {
  let color = 'rgba(51,51,51,1)';
  if (warningState === 'warning') color = '#F5212D';
  if (warningState === 'remind') color = '#FBAF1D';
  if (warningState === 'health') color = '#52C41A';
  if (val) {
    const value = divisor ? _.toNumber(val) / divisor : val;
    return (
      <Tooltip
        placement="top"
        title={
          <Statistic
            value={decimal !== undefined ? value.toFixed(decimal) : value}
            suffix={suffix}
            valueStyle={{ color: '#fff' }}
          />
        }
      >
        <div className="ellipsis text-ellipsis">
          <Statistic
            value={decimal !== undefined ? value.toFixed(decimal) : value}
            suffix={suffix}
            valueStyle={{
              color,
              width: '100%',
              overflow: 'hidden',
              textOverflow: 'ellipsis',
              whiteSpace: 'nowrap',
            }}
          />
        </div>
      </Tooltip>
    );
  }
  return <div>-</div>;
};

const restructureIndexData = (obj, orgType) => {
  const { produceVo, saleVo, statementVo, stockVo, storeVo, supplyVo, returnVo } = obj;
  const items = [];
  const group = orgType === '11'; // 集团
  const region = orgType === '20'; // 区域
  const cityCompany = orgType === '21'; // 城市公司
  const project = orgType === '31'; // 项目
  items.push(
    {
      className: 'store',
      text: '储',
      key: 'store',
      warningCondition: storeVo.earlyWarning,
      warningRegion: storeVo.orgRoundList,
      isDisplay: group || region || cityCompany,
      list: [
        {
          title: '静态储备周期',
          detail: dataMartMetrics.store.jtcbzq,
          isDisplay: group || region || cityCompany,
          num: setStatistic(
            storeVo.staticResvCycle,
            1,
            undefined,
            '年',
            storeVo.staticResvCycle_status,
          ),
        },
        {
          title: '土储货值|投资口径',
          detail: '土储货值|投资口径',
          isDisplay: group || region || cityCompany,
          num: setStatistic(storeVo.soilStoreValueInv, 100000000, 2, '亿元'),
        },
        {
          title: '土储货值|香港口径',
          detail: '土储货值|香港口径',
          isDisplay: group,
          num: setStatistic(storeVo.soilStoreValueHk, 100000000, 2, '亿元'),
        },
        {
          title: '已售未结货值',
          detail: '已售未结货值',
          isDisplay: group || region,
          num: setStatistic(storeVo.soldNotSettle, 100000000, 2, '亿元'),
        },
      ],
    },
    {
      className: 'produce',
      text: '产',
      key: 'product',
      warningCondition: produceVo.earlyWarning,
      warningRegion: produceVo.orgRoundList,
      isDisplay: group || region || cityCompany || project,
      list: [
        {
          title: '首开项目开销比',
          detail: dataMartMetrics.product.skxmkxb,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            produceVo.firstOpenOvhdRate,
            1,
            2,
            '月',
            produceVo.firstOpenOvhdRate_status,
          ),
        },
        {
          title: '续销项目在途比',
          detail: dataMartMetrics.product.xxxmztb,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            produceVo.renewalInTransitRate,
            1,
            2,
            '月',
            produceVo.renewalInTransitRate_status,
          ),
        },
        {
          title: '在途资源',
          detail: '在途资源',
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(produceVo.inTransitResrc, 100000000, 2, '亿元'),
        },
      ],
    },
    {
      className: 'supply',
      text: '供',
      key: 'supply',
      isDisplay: group || region || cityCompany || project,
      list: [
        {
          title: '年度供应达成率',
          detail: dataMartMetrics.supply.ndgydcl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(supplyVo.yearSupplyReachRate, 1, 2, '%'),
        },
        {
          title: '月度供应达成率',
          detail: dataMartMetrics.supply.ydgydcl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(supplyVo.monthSupplyReachRate, 1, 2, '%'),
        },
        {
          title: '年度累计供货',
          detail: dataMartMetrics.supply.ndljgh,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(supplyVo.yearSupply, 100000000, 2, '亿'),
        },
        {
          title: '季度累计供货',
          detail: dataMartMetrics.supply.jdljgh,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(supplyVo.seasonSupply, 100000000, 2, '亿'),
        },
        {
          title: '月度实际供货',
          detail: dataMartMetrics.supply.ydsjgh,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(supplyVo.monthSupply, 100000000, 2, '亿'),
        },
      ],
    },
    {
      className: 'market',
      text: '销',
      key: 'sale',
      isDisplay: group || region || cityCompany || project,
      warningCondition: saleVo.earlyWarning,
      warningRegion: saleVo.orgRoundList,
      list: [
        {
          title: '新供去化率',
          detail: dataMartMetrics.sale.xgqhl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            saleVo.newSupplyRemovalRate,
            1,
            2,
            '%',
            saleVo.newSupplyRemovalRate_status,
          ),
        },
        {
          title: '库存去化率',
          detail: dataMartMetrics.sale.kcqhl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(saleVo.stockRemovalRate, 1, 2, '%', saleVo.stockRemovalRate_status),
        },
        {
          title: '长期库存去化率',
          detail: dataMartMetrics.sale.cqkcqhl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            saleVo.stockRemovalRatelongtime,
            1,
            2,
            '%',
            saleVo.stockRemovalRatelongtime_status,
          ),
        },
      ],
    },
    {
      className: 'deposit',
      text: '存',
      key: 'deposit',
      warningCondition: stockVo.earlyWarning,
      warningRegion: stockVo.orgRoundList,
      isDisplay: group || region || cityCompany || project,
      list: [
        {
          title: '库存',
          detail: dataMartMetrics.deposit.kc,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(stockVo.store, 100000000, 2, '亿元'),
        },
        {
          title: '真实库存',
          detail: dataMartMetrics.deposit.zskc,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(stockVo.realStore, 100000000, 2, '亿元'),
        },
        {
          title: '存销比',
          detail: dataMartMetrics.deposit.cxb,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(stockVo.storeSaleRate, 1, 2, '月', stockVo.storeSaleRate_status),
        },
        {
          title: '长期库存',
          detail: dataMartMetrics.deposit.cqkc,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            stockVo.storeLongTime,
            100000000,
            2,
            '亿元',
            stockVo.storeLongTime_status,
          ),
        },
        {
          title: '长期库存存销比',
          detail: dataMartMetrics.deposit.cqkccxb,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            stockVo.storeSaleRatelongtime,
            1,
            2,
            '月',
            stockVo.storeSaleRatelongtime_status,
          ),
        },
      ],
    },
    {
      className: 'junction',
      text: '结',
      key: 'finish',
      warningCondition: statementVo.earlyWarning,
      warningRegion: statementVo.orgRoundList,
      isDisplay: group || region || cityCompany || project,
      list: [
        {
          title: '可结转资源',
          detail: dataMartMetrics.finish.kjzzy,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(statementVo.statementValue, 100000000, 2, '亿元'),
        },
        {
          title: '结转去化率',
          detail: dataMartMetrics.finish.jzqhl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(statementVo.statementRate, 1, 2, '%', statementVo.statementRate_status),
        },
        {
          title: '业绩锁定率',
          detail: dataMartMetrics.finish.yjsdl,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(statementVo.achievementLockRate, 1, 2, '%'),
        },
      ],
    },
    {
      className: 'return',
      text: '回',
      key: 'return',
      warningCondition: returnVo.earlyWarning,
      warningRegion: returnVo.orgRound,
      isDisplay: group || region || cityCompany || project,
      list: [
        {
          title: '年度签约回款率',
          detail: dataMartMetrics.return.returnYearSign,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(returnVo.returnYearSign, 1, 2, '%', returnVo.returnYearSign_status),
        },
        {
          title: '年度综合回款率',
          detail: dataMartMetrics.return.returnAllYearSign,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(
            returnVo.returnAllYearSign,
            1,
            2,
            '%',
            returnVo.returnAllYearSign_status,
          ),
        },
        {
          title: '滚动综合回款率',
          detail: dataMartMetrics.return.rollReturn,
          isDisplay: group || region || cityCompany || project,
          num: setStatistic(returnVo.rollReturn, 1, 2, '%', returnVo.rollReturn_status),
        },
      ],
    },
  );
  return items;
};
const restructureDetailsData = (data, type, orgType) => {
  const group = orgType === '11'; // 集团
  const region = orgType === '20'; // 区域
  const cityCompany = orgType === '21'; // 城市公司
  const project = orgType === '31'; // 城市公司
  const detailsItems = [];
  if (type === 'store') {
    detailsItems.push(
      {
        key: 'staticResvCycle',
        name: '静态储备周期',
        company: '年',
        isDisplay: group || region || cityCompany,
        icon: {
          iconBg: 'rgba(255,231,233,1)',
          iconType: 'iconzhouqi',
          subIconBg: 'rgba(255,100,115,1)',
        },
        value: setStatistic(
          data.storeVo.staticResvCycle,
          1,
          undefined,
          undefined,
          data.storeVo.staticResvCycle_status,
        ),
        list: data.staticList || [],
      },
      {
        key: 'dynamicResvCycle',
        name: '动态储备周期',
        company: '年',
        isDisplay: group || region || cityCompany,
        icon: {
          iconBg: 'rgba(254,248,227,1)',
          iconType: 'iconzhouqi',
          subIconBg: '#FFB905',
        },
        value: setStatistic(
          data.storeVo.dynamicResvCycle,
          1,
          undefined,
          undefined,
          data.storeVo.dynamicResvCycle_status,
        ),
        list: data.dynamicList || [],
      },
      {
        key: 'soilStoreValueInv',
        name: '土储货值|投资口径',
        company: '亿',
        isDisplay: group || region || cityCompany,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconjiudian',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(data.storeVo.soilStoreValueInv, 100000000, 2, undefined),
        list: data.investmentList || [],
      },
      {
        key: 'soilStoreValueHk',
        name: '土储货值|香港口径',
        isDisplay: group,
        company: '亿',
        icon: {
          iconBg: 'rgba(255,231,233,1)',
          iconType: 'iconjiudian',
          subIconBg: 'rgba(255,100,115,1)',
        },
        value: setStatistic(data.storeVo.soilStoreValueHk, 100000000, 2, undefined),
        list: data.hongKongList || [],
      },
      {
        key: 'soldNotSettle',
        name: '已售未结货值',
        company: '亿',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconweijie',
          subIconBg: '#886BE3',
        },
        value: setStatistic(data.storeVo.soldNotSettle, 100000000, 2, undefined),
        list: data.unsoldList || [],
      },
    );
  }
  if (type === 'product') {
    detailsItems.push(
      {
        key: 'firstOpenOvhdRate',
        name: '首开项目开销比',
        company: '月',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconhuoche1',
          subIconBg: '#FF6473',
        },
        value: setStatistic(
          data.produceVo.firstOpenOvhdRate,
          1,
          undefined,
          undefined,
          data.produceVo.firstOpenOvhdRate_status,
        ),
        list: data.firstOpenOvhdRateList || [],
      },
      {
        key: 'renewalInTransitRate',
        name: '续销项目在途比',
        company: '月',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconhuoche1',
          subIconBg: '#FFB905',
        },
        value: setStatistic(
          data.produceVo.renewalInTransitRate,
          1,
          undefined,
          undefined,
          data.produceVo.renewalInTransitRate_status,
        ),
        list: data.renewalInTransitRateList || [],
      },
      {
        key: 'inTransitResrc',
        name: '在途资源',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconjianzhushigong',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(data.produceVo.inTransitResrc, 100000000, 2, undefined),
        list: data.inTransitResrcList || [],
      },
      {
        key: 'firstOpen',
        name: '首开在途',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E7F2FC',
          iconType: 'iconjianzhushigong',
          subIconBg: '#3484F6',
        },
        value: setStatistic(data.produceVo.firstOpen, 100000000, 2, undefined),
        list: data.firstOpenList || [],
      },
      {
        key: 'continueSale',
        name: '续销在途',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconjianzhushigong',
          subIconBg: '#886BE3',
        },
        value: setStatistic(data.produceVo.continueSale, 100000000, 2, undefined),
        list: data.continueSaleList || [],
      },
    );
  }
  if (type === 'supply') {
    detailsItems.push(
      {
        key: 'halfYearSupplyRate',
        name: '上下半年供应比',
        company: '',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconbili',
          subIconBg: '#FF6473',
        },
        value: setStatistic(
          `${data.supplyVo.firstHalfYearSupplyRate || '-'}:${data.supplyVo
            .secondHalfYearSupplyRate || '-'}`,
          undefined,
          undefined,
          undefined,
          data.supplyVo.halfYearSupplyRate_status,
        ),
        list: data.halfYearSupplyRateList || [],
      },
      {
        key: 'yearSupplyReachRate',
        name: '年度供应达成率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconxianjinbishuai',
          subIconBg: '#FFB905',
        },
        value: setStatistic(data.supplyVo.yearSupplyReachRate, 1, undefined, undefined),
        list: data.yearSupplyReachRateList || [],
      },
      {
        key: 'seasonSupplyReachRate',
        name: '季度供应达成率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconxianjinbishuai',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(data.supplyVo.seasonSupplyReachRate, 1, undefined, undefined),
        list: data.seasonSupplyReachRateList || [],
      },
      {
        key: 'monthSupplyReachRate',
        name: '月度供应达成率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E7F2FC',
          iconType: 'iconxianjinbishuai',
          subIconBg: '#3484F6',
        },
        value: setStatistic(data.supplyVo.monthSupplyReachRate, 1, undefined, undefined),
        list: data.monthSupplyReachRateList || [],
      },
      {
        key: 'yearSupply',
        name: '年度累计供货',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconxianjinbishuai',
          subIconBg: '#FF6473',
        },
        value: setStatistic(data.supplyVo.yearSupply, 100000000, 2, undefined),
        list: data.yearSupplyList || [],
      },
      {
        key: 'seasonSupply',
        name: '季度累计供货',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconxianjinbishuai',
          subIconBg: '#FFB905',
        },
        value: setStatistic(data.supplyVo.seasonSupply, 100000000, 2, undefined),
        list: data.seasonSupplyList || [],
      },
      {
        key: 'monthSupply',
        name: '月度实际供货',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: 'rgba(255,231,233,1)',
          iconType: 'iconxianjinbishuai',
          subIconBg: 'rgba(255,100,115,1)',
        },
        value: setStatistic(data.supplyVo.monthSupply, 100000000, 2, undefined),
        list: data.monthSupplyList || [],
      },
    );
  }
  if (type === 'sale') {
    detailsItems.push(
      {
        key: 'firstOpenRate',
        name: '首开去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconloudou',
          subIconBg: '#FF6473',
        },
        value: setStatistic(
          data.saleVo.firstOpenRate,
          1,
          undefined,
          undefined,
          data.saleVo.firstOpenRate_status,
        ),
        list: data.firstOpenRateList || [],
      },
      {
        key: 'monthRate',
        name: '月度去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconloudou',
          subIconBg: '#FFB905',
        },
        value: setStatistic(
          data.saleVo.monthRate,
          1,
          undefined,
          undefined,
          data.saleVo.monthRate_status,
        ),
        list: data.monthRateList || [],
      },
      {
        key: 'newSupplyRemovalRate',
        name: '新供去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconloudou',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(
          data.saleVo.newSupplyRemovalRate,
          1,
          undefined,
          undefined,
          data.saleVo.newSupplyRemovalRate_status,
        ),
        list: data.newSupplyRemovalRateList || [],
      },
      {
        key: 'stockRemovalRate',
        name: '库存去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E7F2FC',
          iconType: 'iconloudou',
          subIconBg: '#3484F6',
        },
        value: setStatistic(
          data.saleVo.stockRemovalRate,
          1,
          undefined,
          undefined,
          data.saleVo.stockRemovalRate_status,
        ),
        list: data.stockRemovalRateList || [],
      },
      {
        key: 'stockRemovalRatelongtime',
        name: '长期库存去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconloudou',
          subIconBg: '#886BE3',
        },
        value: setStatistic(
          data.saleVo.stockRemovalRatelongtime,
          1,
          undefined,
          undefined,
          data.saleVo.stockRemovalRatelongtime_status,
        ),
        list: data.stockRemovalRatelongtimeList || [],
      },
      {
        key: 'saleRateExit',
        name: '现房库存去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconloudou',
          subIconBg: '#886BE3',
        },
        value: setStatistic(
          data.saleVo.saleRateExit,
          1,
          undefined,
          undefined,
          data.saleVo.saleRateExit_status,
        ),
        list: data.saleRateExitList || [],
      },
      {
        key: 'monthActualSale',
        name: '月度实际销售',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconloudou',
          subIconBg: '#886BE3',
        },
        value: setStatistic(data.saleVo.monthActualSale, 100000000, 2, undefined),
        list: data.monthActualSaleList || [],
      },
      {
        key: 'monthSaleRate',
        name: '月度销售达成率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconloudou',
          subIconBg: '#886BE3',
        },
        value: setStatistic(data.saleVo.monthSaleRate, 1, undefined, undefined),
        list: data.monthSaleRateList || [],
      },
    );
  }
  if (type === 'deposit') {
    detailsItems.push(
      {
        key: 'store',
        name: '库存',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: 'rgba(255,231,233,1)',
          iconType: 'iconcunhuo',
          subIconBg: 'rgba(255,100,115,1)',
        },
        value: setStatistic(data.stockVo.store, 100000000, 2, undefined),
        list: data.storeList || [],
      },
      {
        key: 'hasNotToken',
        name: '已达未取',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconweijie',
          subIconBg: '#FFB905',
        },
        value: setStatistic(data.stockVo.hasNotToken, 100000000, 2, undefined),
        list: data.hasNotTokenList || [],
      },
      {
        key: 'realStore',
        name: '真实库存',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconkucun',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(data.stockVo.realStore, 100000000, 2, undefined),
        list: data.realStoreList || [],
      },
      {
        key: 'storeSaleRate',
        name: '存销比',
        company: '月',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E7F2FC',
          iconType: 'iconshijianbijiao',
          subIconBg: '#3484F6',
        },
        value: setStatistic(
          data.stockVo.storeSaleRate,
          1,
          undefined,
          undefined,
          data.stockVo.storeSaleRate_status,
        ),
        list: data.storeSaleRateList || [],
      },
      {
        key: 'storeRatelongTime',
        name: '长期库存占比',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconzhibiaox',
          subIconBg: '#886BE3',
        },
        value: setStatistic(
          data.stockVo.storeRatelongTime,
          1,
          undefined,
          undefined,
          data.stockVo.storeRatelongTime_status,
        ),
        list: data.storeRatelongTimeList || [],
      },
      {
        key: 'storeAdd',
        name: '新增长期库存',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#F0EBFF',
          iconType: 'iconzhibiao3',
          subIconBg: '#886BE3',
        },
        value: setStatistic(
          data.stockVo.storeAdd,
          100000000,
          2,
          undefined,
          data.stockVo.storeAdd_status,
        ),
        list: data.storeAddList || [],
      },
      {
        key: 'storeLongTime',
        name: '长期库存',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E7F2FC',
          iconType: 'iconcangku',
          subIconBg: '#3484F6',
        },
        value: setStatistic(
          data.stockVo.storeLongTime,
          100000000,
          2,
          undefined,
          data.stockVo.storeLongTime_status,
        ),
        list: data.storeLongTimeList || [],
      },
      {
        key: 'storeSaleRatelongtime',
        name: '长期库存存销比',
        company: '月',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: 'rgba(255,231,233,1)',
          iconType: 'iconshijianbijiao',
          subIconBg: 'rgba(255,100,115,1)',
        },
        value: setStatistic(
          data.stockVo.storeSaleRatelongtime,
          1,
          undefined,
          undefined,
          data.stockVo.storeSaleRatelongtime_status,
        ),
        list: data.storeSaleRatelongtimeList || [],
      },
      {
        key: 'storeExist',
        name: '现房库存',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconyihuoquxiangmudezonghuozhi',
          subIconBg: '#FFB905',
        },
        value: setStatistic(
          data.stockVo.storeExist,
          100000000,
          2,
          undefined,
          data.stockVo.storeExist_status,
        ),
        list: data.storeExistList || [],
      },
      {
        key: 'storeSaleRateexit',
        name: '现房库存存销比',
        company: '月',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconshijianbijiao',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(
          data.stockVo.storeSaleRateexit,
          1,
          undefined,
          undefined,
          data.stockVo.storeSaleRateexit_status,
        ),
        list: data.storeSaleRateexitList || [],
      },
    );
  }
  if (type === 'finish') {
    detailsItems.push(
      {
        key: 'statementValue',
        name: '可结转资源',
        company: '亿元',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconbiaoqian',
          subIconBg: '#FF6473',
        },
        value: setStatistic(data.statementVo.statementValue, 100000000, 2, undefined),
        list: data.statementValueList || [],
      },
      {
        key: 'statementRate',
        name: '结转去化率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FEF8E3',
          iconType: 'iconloudou',
          subIconBg: '#FFB905',
        },
        value: setStatistic(
          data.statementVo.statementRate,
          1,
          undefined,
          undefined,
          data.statementVo.statementRate_status,
        ),
        list: data.statementRateList || [],
      },
      {
        key: 'achievementLockRate',
        name: '业绩锁定率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#E0FCDA',
          iconType: 'iconAPP-chengshigongsizhibiao',
          subIconBg: '#59CC3E',
        },
        value: setStatistic(data.statementVo.achievementLockRate, 1, undefined, undefined),
        list: data.achievementLockRateList || [],
      },
    );
  }
  if (type === 'return') {
    detailsItems.push(
      {
        key: 'returnYearSign',
        name: '年度签约回款率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconloudou',
          subIconBg: '#FF6473',
        },
        value: setStatistic(
          data.returnVo.returnYearSign,
          1,
          2,
          '%',
          data.returnVo.returnYearSign_status,
        ),
        list: data.returnYearSignList || [],
      },
      {
        key: 'returnAllYearSign',
        name: '年度综合回款率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconloudou',
          subIconBg: '#FF6473',
        },
        value: setStatistic(
          data.returnVo.returnAllYearSign,
          1,
          2,
          '%',
          data.returnVo.returnAllYearSign_status,
        ),
        list: data.returnAllYearSignList || [],
      },
      {
        key: 'rollReturn',
        name: '滚动综合回款率',
        company: '%',
        isDisplay: group || region || cityCompany || project,
        icon: {
          iconBg: '#FFE7E9',
          iconType: 'iconloudou',
          subIconBg: '#FF6473',
        },
        value: setStatistic(data.returnVo.rollReturn, 1, 2, '%', data.returnVo.rollReturn_status),
        list: data.rollReturnList || [],
      },
    );
  }
  return detailsItems;
};
const buildTopData = (obj, orgType) => {
  const list = [];
  if (orgType === '31') {
    list.push(
      {
        className: 'produce',
        title: '总货值(亿)',
        value: obj.totalPrice ? _.toNumber(obj.totalPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益总货值',
            value: obj.equityPrice ? _.toNumber(obj.equityPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '阶段自持货值',
            value: obj.stagePrice ? _.toNumber(obj.stagePrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
        ],
      },
      {
        className: 'deposit',
        title: '规划货值(亿)',
        value: obj.planPrice ? _.toNumber(obj.planPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益在途货值',
            value: obj.planEquityPrice ? _.toNumber(obj.planEquityPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.planEquityRate ? _.toNumber(obj.planEquityRate) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'junction',
        title: '土储货值|香港口径(亿)',
        value: obj.soilStoreHk ? _.toNumber(obj.soilStoreHk / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益土储货值',
            value: obj.soilStoreEquityHk ? _.toNumber(obj.soilStoreEquityHk / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.soilStoreEquityRateHk ? _.toNumber(obj.soilStoreEquityRateHk) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        title: '土储货值|投资口径(亿)',
        value: obj.soilStoreInv ? _.toNumber(obj.soilStoreInv / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益土储货值',
            value: obj.soilStoreEquityInv ? _.toNumber(obj.soilStoreEquityInv / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.soilStoreEquityRateInv ? _.toNumber(obj.soilStoreEquityRateInv) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'supply',
        title: '已售货值(亿)',
        value: obj.saledPrice ? _.toNumber(obj.saledPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益已售货值',
            value: obj.stageSaledPrice ? _.toNumber(obj.stageSaledPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.stageRate ? _.toNumber(obj.stageRate) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'market',
        title: '存货货值(亿)',
        value: obj.stockPrice ? _.toNumber(obj.stockPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益存货货值',
            value: obj.stockEquityPrice ? _.toNumber(obj.stockEquityPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.stockEquityPriceReat ? _.toNumber(obj.stockEquityPriceReat) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
    );
  } else {
    list.push(
      {
        title: '项目总数(个)',
        value: obj.projectCount ? _.toNumber(obj.projectCount) : 0,
        precision: 0,
        content: [
          {
            title: '首开项目',
            value: obj.firstOpen ? _.toNumber(obj.firstOpen) : 0,
            suffix: '个',
            precision1: 0,
          },
          {
            title: '续销项目',
            value: obj.continueCount ? _.toNumber(obj.continueCount) : 0,
            suffix: '个',
            precision1: 0,
          },
        ],
      },
      {
        className: 'market',
        title: '存货货值(亿)',
        value: obj.stockPrice ? _.toNumber(obj.stockPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益存货货值',
            value: obj.stockEquityPrice ? _.toNumber(obj.stockEquityPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.stockEquityPriceReat ? _.toNumber(obj.stockEquityPriceReat) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'deposit',
        title: '规划货值(亿)',
        value: obj.planPrice ? _.toNumber(obj.planPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益在途货值',
            value: obj.planEquityPrice ? _.toNumber(obj.planEquityPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.planEquityRate ? _.toNumber(obj.planEquityRate) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'junction',
        title: '土储货值|投资口径(亿)',
        value: obj.soilStoreInv ? _.toNumber(obj.soilStoreInv / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益土储货值',
            value: obj.soilStoreEquityInv ? _.toNumber(obj.soilStoreEquityInv / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.soilStoreEquityRateInv ? _.toNumber(obj.soilStoreEquityRateInv) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'produce',
        title: '总货值(亿)',
        value: obj.totalPrice ? _.toNumber(obj.totalPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益总货值',
            value: obj.equityPrice ? _.toNumber(obj.equityPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '阶段自持货值',
            value: obj.stagePrice ? _.toNumber(obj.stagePrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
        ],
      },
      {
        className: 'supply',
        title: '已售货值(亿)',
        value: obj.saledPrice ? _.toNumber(obj.saledPrice / 100000000) : 0,
        precision: 2,
        content: [
          {
            title: '权益已售货值',
            value: obj.stageSaledPrice ? _.toNumber(obj.stageSaledPrice / 100000000) : 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: obj.stageRate ? _.toNumber(obj.stageRate) : 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
    );
  }
  return list;
};
const setStoreSaleRateData = obj => {
  const list = [];
  _.each(obj, item => {
    list.push({
      name: item.name,
      company: '%',
      value: setStatistic(item.rate, 1, 2, ''),
      isDisplay: true,
      icon: null,
    });
  });
  return list;
};
const Model = {
  namespace: 'MonitoringConsole',
  state: {
    arr: [1],
    tree: [],
    orgIds: {},
    topData: [],
    bottomData: [],
    problemAreaName: [],
    detailsItems: [],
    storeSaleRateData: [],
  },
  effects: {
    *init({ payload, callback }, { call, put }) {
      const result = yield call(apis.getMonitoringUserDataPermissionsV2UsingGET, payload);
      if (result && result.code === 1) {
        yield put({
          type: 'save',
          payload: { tree: _.unionBy(result.data, 'orgdepid') },
        });
        callback(result.data);
      }
    },
    *requestIndexinit({ payload }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: { topData: [], bottomData: [], problemAreaName: [] },
      });
      const orgIds = yield select(state => state.MonitoringConsole.orgIds);
      const { hrOrgTypeId } = orgIds;
      const topResult = yield call(apis.getValueMonitoringUsingGET, { params: payload.params1 });
      if (topResult && topResult.code === 1) {
        const topData = buildTopData(topResult.data, hrOrgTypeId);
        yield put({
          type: 'save',
          payload: { topData },
        });
      }
      // ########核心指标监控##########
      const bottomResult = yield call(apis.getCoreQuotaStoreUsingGET, { params: payload.params2 });
      if (bottomResult && bottomResult.code === 1) {
        // problemAreaName 问题区域名
        const problemAreaName = bottomResult.data.existingProblems;
        // 清洗过后的数据
        const bottomData = restructureIndexData(bottomResult.data, hrOrgTypeId);
        yield put({
          type: 'save',
          payload: { bottomData, problemAreaName },
        });
      }
    },
    *requestDetalsinit({ payload }, { call, put, select }) {
      const orgIds = yield select(state => state.MonitoringConsole.orgIds);
      const { hrOrgTypeId, page } = orgIds;
      yield put({
        type: 'save',
        payload: { detailsItems: [] },
      });
      let urlName;
      if (page === 'store') urlName = apis.getStoreItemUsingGET;
      if (page === 'product') urlName = apis.getProduceItemUsingGET;
      if (page === 'supply') urlName = apis.getSupplyItemUsingGET;
      if (page === 'sale') urlName = apis.getSaleItemUsingGET;
      if (page === 'deposit') urlName = apis.getStockItemUsingGET;
      if (page === 'finish') urlName = apis.getStatementItemUsingGET;
      if (page === 'return') urlName = apis.getReturnItemUsingGET;
      const result = yield call(urlName, { params: payload.params });
      if (result && result.code === 1) {
        const detailsItems = restructureDetailsData(result.data, page, hrOrgTypeId);
        yield put({
          type: 'save',
          payload: { detailsItems },
        });
      }
    },
    *getStoreSaleRate({ payload }, { call, put }) {
      const result = yield call(apis.getStoreSaleRateUsingGET, { params: payload.params });
      if (result && result.code === 1) {
        const storeSaleRateData = setStoreSaleRateData(result.data);
        yield put({
          type: 'save',
          payload: { storeSaleRateData },
        });
      }
    },
    *getOrgIds({ payload }, { put }) {
      yield put({
        type: 'save',
        payload: { orgIds: payload.orgIds, getTree: payload.getTree },
      });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default Model;
